# -*- coding: utf-8 -*-
import logging
from fastapi import APIRouter

from schemas.document import Document
from schemas.response import Response
from services.document_service import DocumentService

logger = logging.getLogger(__name__)
router = APIRouter(prefix="")


@router.post("/writer")
async def insert_data(document: Document):
    """
    插入文档片段数据
    """
    try:
        document_id, error = await DocumentService.insert_or_update(document.dict())
        return Response.success(message="文档片段保存成功", data={"document_id": document_id})
    except Exception as e:
        return Response.fail(message=f"插入文档片段数据失败: {str(e)}")


@router.get("/get_data")
async def read(document_id: str):
    """
    获取文档片段数据
    """
    try:
        data = await DocumentService.get_document(document_id)
        if not data:
            return Response.fail(message="文档片段不存在或已被删除")
        return Response.success(data=data)
    except Exception as e:
        return Response.fail(message=f"获取文档片段数据失败: {str(e)}")

